package main

import "sync"

var totol int
var wg sync.WaitGroup
var lock sync.Mutex
// 互斥锁

// 读写锁

func p1(i int) {
	defer wg.Done()
	lock.Lock()
	totol = totol + 1
	lock.Unlock()
	println(totol)
}

func p2(i int) {
	defer wg.Done()
	lock.Lock()
	totol = totol - 1
	lock.Unlock()
	println(totol)
}

func add1()  {
	for i := 0; i < 10; i ++ {
		wg.Add(1)
		go p1(i)
	}
}

func add2()  {
	for i := 0; i < 10; i ++ {
		wg.Add(1)
		go p2(i)
	}
}

func main() {
	add1()
	add2()
	wg.Wait()
}
